package cn.shiwei.user.controller;

import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.user.domain.Logininfo;
import cn.shiwei.user.query.LogininfoQuery;
import cn.shiwei.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class LogininfoController {
    
    @Autowired
    private ILogininfoService logininfoService;

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门",notes = "需要参数id")
    private Logininfo queryById(@PathVariable("id") Long id){
        return logininfoService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询全部",notes = "无参数")
    private List<Logininfo> queryAll(){
        return logininfoService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "部门列表分页查询",notes = "需要传参 为高级查询的条件")
    private PageList<Logininfo> queryPage(@RequestBody LogininfoQuery query){
        return logininfoService.queryDate(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除删除",notes = "需要参数id")
    private RequestStatus delete(@PathVariable("id") Long id){
        try {
            logininfoService.remove(id);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PutMapping()
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    private  RequestStatus addAndUpdate(@RequestBody Logininfo logininfo){
        try {
            if(logininfo.getId() == null){
                logininfoService.save(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("操作失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除",notes = "需要参数id")
    private RequestStatus patchDelete(@RequestBody List<Long> ids){
        try {
            logininfoService.patchRemove(ids);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }
}
